<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <script src="/jquery/jquery-3.6.0.js"></script>
    <script src="/util/request.js"></script>

    <style>
        input[type="text"],
        input[type="password"],
        input[type="number"],
        button {
            border-radius: 8px;
        }
        .login-container input,
        .login-container button {
            border-radius: 8px;
        }
        body {
            background: url("static/image/image.png") no-repeat;
            background-size:100%;
        }
        .login-container {
            background-color: rgba(255, 255, 255, 0.5);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
            width: 400px; /* 控制登录框宽度 */
            position: absolute;
            top: 35%;
            left: 13%;
        }
        .login-title {
            text-align: center;
            margin-bottom: 20px;
            font-size: 24px;
        }
        .captcha-container {
            display: flex;
            align-items: center;
        }
        #captchaImg {
            cursor: pointer;
            margin-left: 10px;
        }
    </style>

</head>
<body>
<div class="login-container layui-anim layui-anim-scale">
    <div class="login-title">登录</div>
    <form class="layui-form" >
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label custom-padding">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-block captcha-container">
                <input type="text" name="captcha" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
                <img style="width: 85px;height: 35px;border-radius: 10px" id="captchaImg" src="" alt="验证码" onclick="reloadCaptcha()">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="login" style="width: 290px">登录</button>
            </div>
        </div>
    </form>
</div>

<script>
    var codeKey = '';
    layui.use(['form'], function() {

        var form = layui.form;


        // 监听提交
        form.on('submit(login)', function(data) {

            const formData = {
                username: data.field.username,
                password: data.field.password,
                code: data.field.captcha,
                codeKey: codeKey
            };


            postData('/public/login', formData, true).then(res => {
                const token=res.token;
                const admin=res.data;
                sessionStorage.setItem('token', token)
                localStorage.setItem('admin', JSON.stringify(admin));
                    // window.location.href = '/index.html';
                if(admin.faceRegister===0){
                    window.location.href = '/page/index.html';
                }
                if(admin.faceRegister!==0){
                    window.location.href = '/page/checkFace.html';
                }

            }).catch(err => {
                console.error(err);
            });
            return false;
        });

        // 加载验证码
        reloadCaptcha();
    });

    // 刷新验证码
    function reloadCaptcha() {
        getData('/public/generateValidateCode', {}, true).then(res => {
            const data = res.data;  // 直接使用 res.data 对象
            codeKey = data.codeKey;
            document.getElementById('captchaImg').src =data.codeValue;
        }).catch(err => {
            console.error(err);
        });
    }

</script>
</body>
</html>